import $ from '../../utils/ajax.js';
import util from '../../utils/util.js';
import Base64 from '../../utils/base64.js';
const app = getApp();
Page({
    data: {
        loginObject: {
            tel: '',
            pass: '',
            code: ''
        },
        codeObject: {
            codeTime: 60,
            codeText: '',
            codeBtn: false
        },
        setInter: '',
        referId: '',
        /***
         * 登录方式状态,
         * 登录按钮
         */
        actionState: false,
        open: false,
        localRandom: 'M!vw@lFNcOv17Us*yTvyRBHXfbjztDQ1py$*C^w46aRjbOmW3Rc2%MkB38hdO1hpkENwW7^UJ#0foKSH&4ZQX5@VfdASrCLxhG%',
        localRandomes: 'DontTellOtherpeopleplzas1238fdja0hghn',
        base64: '',
        encryption: '',
        phone: '',
        send:true
    },
    onLoad(options) {
        wx.clearStorage()
    },
    //跳转注册页面
    reset() {
        wx.navigateTo({
            url: '/pages/login/reset/reset',
        })
    },
    // 切换密码登录
    switchover() {
        this.setData({
            actionState: false
        })
    },
    //切换验证码登录
    switchovers() {
        this.setData({
            actionState: true
        })
    },
    //获取手机号
    phoneChange(e) {
        const phone = e.detail.value
        const localRandom = this.data.localRandomes;
        let phonese = util.Encrypts(phone, localRandom);
   
        this.setData({
            ['loginObject.tel']: phone,
            phone: phonese,
          
        })
        if (phone.length > 0) {
            this.setData({
                open: true
            })
        } else {
            this.setData({
                open: false
            })
        }
    },
    //获取密码
    passWordChange(e) {
        const password = e.detail.value
        this.setData({
            ['loginObject.pass']: password
        })
    },
    //获取验证码
    codeChange(e) {
        const code = e.detail.value
        this.setData({
            ['loginObject.code']: code
        })
    },

    //验证码
    codeBtn(e) {
        const prime = this.data.loginObject;
        if (util.telCheck(prime.tel,'')) {
            this.setData({
                send: false
            })
            $.p(app.wxApp.url + 'passport/verifyCode', {
                a: this.data.phone,
            }, app.onStrart, (res) => {
                util.showToast('发送成功')
                this.data.setInter = setInterval(() => {
                    let time = this.data.codeObject.codeTime - 1;
                    let text = time + 's';
                    let btn = true;
                    if (time === 0) {
                        time = 60;
                        text = '';
                        btn = false;
                        this.setData({
                            send:true
                        })
                        clearInterval(this.data.setInter);
                    }
                    this.setData({
                        codeObject: {
                            codeTime: time,
                            codeText: text,
                            codeBtn: btn
                        }
                    })
                }, 1000)
            }, (res) => {
                util.showToast(res.data)
            })

        }else {
            this.setData({
                send: true
            })
        }
    },
    onLogin() {
        const prime = this.data.loginObject
        const pass = this.data.loginObject.pass
        const localRandom = this.data.localRandom
        let password = util.Encrypt(pass, localRandom)
        // 获取加密因子
        $.p(app.wxApp.url + 'passport/getServerRandom', {
            mobileNo: this.data.phone
        }, app.onStrart, (res) => {
            let base64 = Base64.base64_decode(res.data.serverRandom)
            const encryption = util.Encrypt(password, base64)
            this.setData({
                encryption: encryption
            })
            if (this.data.actionState == false) {
                this.setData({
                    ['loginObject.code']:''
                })
            } else if (this.data.actionState == true) {
                this.setData({
                    encryption:''
                })
            }
            //把加密后的密码发送给后端
            $.p(app.wxApp.url + 'agentNew/agentLogin', {
                mobileNo: this.data.phone,
                password: this.data.encryption,
                code: this.data.loginObject.code
            }, app.onStrart, (res) => {
                wx.setStorageSync('xfjtk', res.data.ticket);
                wx.setStorageSync('tel', res.data.userPhone);
                wx.setStorageSync('agent', res.data.agentId);
                app.agentStatus()
            }, (res) => {
                util.showToast(res.data)
            })
        }, (res) => {
            util.showToast(res.data)
        })
    }

    

})